package com.cloud.pole.merchant.controller;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.msg.ObjectRestResponse;
import com.cloud.pole.common.util.jwt.JwtAuthenticationRequest;
import com.cloud.pole.merchant.service.AuthService;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("auth")
@Slf4j
public class AuthController {    
    private String tokenHeader = CommonConstants.AUTH_USER_TOKEN_HEADER;
    
    @Autowired
    private AuthService authService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public ObjectRestResponse<String> login(@RequestBody JwtAuthenticationRequest authenticationRequest) throws Exception {
    	log.info(authenticationRequest.getUsername()+" require logging...");
        try {
			ParamException.err(StringUtils.isBlank(authenticationRequest.getUsername()), "用户名不能为空");
			ParamException.err(StringUtils.isBlank(authenticationRequest.getPassword()), "密码不能为空");
			//手机号自动加上86
			if(!authenticationRequest.getUsername().startsWith("86")) {
				authenticationRequest.setUsername("86" + authenticationRequest.getUsername());
			}
	        final String token = authService.login(authenticationRequest);
	        log.info(authenticationRequest.getUsername()+ "token["+ token +"]");
	        return new ObjectRestResponse<>().data(token);
		} catch (BaseException e) {
			return new ObjectRestResponse(e.getStatus(), e.getMessage());
		} catch (Exception e) {
			log.error("登录错误{}", e.getMessage(), e);
			return new ObjectRestResponse(CommonConstants.STATUS_ERROR_CODE, CommonConstants.STATUS_ERROR_MESSAGE);
		}
    }
    
    @RequestMapping(value = "refresh", method = RequestMethod.GET)
    public ObjectRestResponse<String> refreshAndGetAuthenticationToken(
            HttpServletRequest request) throws Exception {
        String token = request.getHeader(tokenHeader);
        String refreshedToken = authService.refresh(token);
        return new ObjectRestResponse<>().data(refreshedToken);
    }

    @RequestMapping(value = "verify", method = RequestMethod.GET)
    public ObjectRestResponse<?> verify(String token) throws Exception {
        authService.validate(token);
        return new ObjectRestResponse<>();
    }
    
    /**
     * 检测用户状态，根据token
     * @param jwtInfo
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/user/validate", method = RequestMethod.GET)
    public @ResponseBody boolean validate(String token) throws Exception{
    	return authService.validate(token);
    }
}
